<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Vehicle;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use App\Admin\Renderable\ActivityTable;
use App\Models\Activity;

class VehicleController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $model = Vehicle::with(['activity', 'series']);

        return Grid::make($model, function (Grid $grid) {
            $grid->model()->orderBy('order');
            $grid->column('id')->sortable();
            $grid->column('series.series_name', '车系')->label();
            $grid->column('activity.title', '活动');
            $grid->column('name');
            $grid->column('code');
            $grid->column('price');
            $grid->column('min_img_url')->image();
            $grid->column('order')->orderable();
            // $grid->column('recommend')->switch();
            // $grid->column('show')->switch();
            $grid->switch_group->switchGroup(['recommend', 'show']);
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->equal('id')->integer()->width(2);
                $filter->like('name')->width(3);
                $filter->equal('vehicle_series_id')->width(3)->select('api/series');
                $filter->equal('recommend')->select([0 => '否', 1 => '是'])->width(2);
            });
            $grid->export()->csv();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        $model = Vehicle::with(['activity', 'series']);

        return Show::make($id, $model, function (Show $show) {
            $show->field('id');
            $show->field('price');
            $show->field('introduction');
            $show->field('series.series_name', '车系');
            $show->field('activity.title', '活动');
            $show->field('name');
            $show->field('code');
            $show->field('img_url')->image();
            $show->field('min_img_url')->image();
            $show->field('describe');
            $show->field('order');
            $show->field('recommend')->using([0 => '否', 1 => '是']);
            $show->field('show')->using([0 => '否', 1 => '是']);
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Vehicle(), function (Form $form) {
            $form->display('id');
            $form->text('introduction');
            $form->decimal('price');
            $form->select('vehicle_series_id')->options('/api/series')->required();
            $form->selectTable('activity_id', '活动')
                ->from(ActivityTable::make(['id' => $form->getKey()]))
                ->title('活动列表')
                ->dialogWidth('50%')
                ->model(Activity::class, 'id', 'title');
            $form->text('name')->required();
            $form->text('code');
            $form->image('img_url')->disk('obs')->removable(false)->uniqueName()->saveFullUrl()->autoUpload()->required();
            $form->image('min_img_url')->disk('obs')->removable(false)->uniqueName()->saveFullUrl()->autoUpload()->required();
            $form->editor('describe')->required();
            // $form->number('order')->required();
            $form->switch('recommend');
            $form->switch('show');
        });
    }
}
